package cn.training.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController{
	@Autowired
	HelloWorldService helloWorldService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        return "denglu";
    }
    
    
    //1查询
    @RequestMapping(value = "init", method = RequestMethod.POST)
    public String initLogin(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.searchUser(UserBean);
    	model.addAttribute("UserBean",result);
        return "login";
    }
    
    
    
    //1插入
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insertLogin(UserBean UserBean,Model model) {
    	if(helloWorldService.insertUser(UserBean)!=0)
    		return "success";
    	else
    		return "helloWorld";
    }
    
    //1通过id查询用户名
    @RequestMapping(value = "name", method = RequestMethod.POST)
    public String nameLogin(UserBean UserBean,Model model) {
    	UserBean result=helloWorldService.searchname(UserBean);
    	model.addAttribute("UserBean",result);
        return "login";
        
    }
    
    
    //1更新用户名
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String nameupdate(UserBean UserBean,Model model) {
        if(helloWorldService.updateUser(UserBean)!=0)
    		return "success";
    	else
    		return "login";
    }
    
    
    //1查询所以用户
    @RequestMapping(value = "all", method = RequestMethod.POST)
    public String selectall(UserBean UserBean,Model model) {
    	List<UserBean> result=helloWorldService.selectall(UserBean);
    	model.addAttribute("list",result);
        return "all";
    }
    
    
    //1删除用户
    @RequestMapping(value = "deleteid", method = RequestMethod.GET)
    public String delete(UserBean UserBean,Model model) {
    	helloWorldService.deleteid(UserBean);
    	//model.addAttribute("UserBean",result);
    	List<UserBean> result1=helloWorldService.selectall(UserBean);
    	model.addAttribute("list",result1);
        return "all";
    }
    
    
    //2用户登录
    @RequestMapping(value = "denglu", method = RequestMethod.POST,params="login")
    public String login(UserB userB, Model model) {
			UserB result = helloWorldService.searchyonghu(userB);
			if(result != null) {
				List<UserB> result1=helloWorldService.allyonghu(userB);
		    	model.addAttribute("list",result1);
		        return "meeage";
			} else {
				model.addAttribute("message", "输入不正确");
				return "denglu";
			}
	}
    
    
    //2点击注册跳转
    @RequestMapping(value = "denglu", method = RequestMethod.POST,params="register")
    public String register(UserB userB, Model model) {
			return "register";
	}
    //2点击返回跳转
    @RequestMapping(value = "register", method = RequestMethod.POST,params="back")
    public String back(UserB userB, Model model) {
			return "denglu";
	}
   
    
    
    //2用户注册
    @RequestMapping(value = "register", method = RequestMethod.POST,params="register")
    public String adduser(HttpServletRequest req,UserB UserB,Model model) {
    	int insert=helloWorldService.adduser(UserB);
    	String password = req.getParameter("password");
        String repassword = req.getParameter("repassword");
        if(password.equals(repassword)){
        	if(insert==1)
        		return "denglu";
        	else{
        		model.addAttribute("UserB", UserB);//保留原来输入的信息
        		return "register";}
        	
        }
        else
        {
        	model.addAttribute("UserB", UserB);//保留原来输入的信息
        	model.addAttribute("message", "密码与确认密码输入不正确");
			return "register";
        }
    }
   
    
    
    //2点击删除链接跳转
    @RequestMapping(value = "deleteyonghu1", method = RequestMethod.GET)
    public String deleteyonghu1(UserB userB, Model model) {
    	UserB result=helloWorldService.selectid(userB);
    	model.addAttribute("UserB",result);  	
	        return "delete";
	}
    
    
    //2点击不删除用户跳转
    @RequestMapping(value = "deleteyonghu", method = RequestMethod.GET,params="nodelete")
    public String no(UserB userB, Model model) {
    	List<UserB> result1=helloWorldService.allyonghu(userB);
    	model.addAttribute("list",result1);
			return "meeage";
	}
    
    //2删除用户
    @RequestMapping(value = "deleteyonghu", method = RequestMethod.GET,params="delete")
    public String deleteyonghu(UserB UserB,Model model) {
    	int deleteyonghu=helloWorldService.deleteyonghu(UserB);
    	model.addAttribute("UserB",deleteyonghu);
    	//model.addAttribute("UserBean",result);
    	List<UserB> result1=helloWorldService.allyonghu(UserB);
    	model.addAttribute("list",result1);
        return "meeage";
    }
    
    
    //2点击name链接跳转
    @RequestMapping(value = "updateName", method = RequestMethod.GET)
    public String updatename(UserB userB, Model model) {
    	UserB result=helloWorldService.selectnameid(userB);
    	model.addAttribute("UserB",result);  	
	        return "update";
	}
//    不验证是否为空的代码
//    @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
//    public String updateyonghu(HttpServletRequest req,UserB UserB,Model model) {
//    	String password = req.getParameter("password");
//        String repassword = req.getParameter("repassword");
//        if(password.equals(repassword)){
//        if(helloWorldService.updateYonghu(UserB)!=0)
//        {
//        	UserB result=helloWorldService.selectid(UserB);
//        	model.addAttribute("UserB",result);  	
//        	List<UserB> result1=helloWorldService.allyonghu(UserB);
//    	    model.addAttribute("list",result1);
//    		return "meeage";
//    		}
//    	else
//    		return "update";
//        }
//        else
//        {
//        	UserB result=helloWorldService.selectnameid(UserB);
//        	model.addAttribute("UserB",result);  	
//        	model.addAttribute("message", "密码与确认密码输入不正确");
//        	return "update";
//        }
//    }
    //2、点击更新通过判断，更新操作后跳转
    @RequestMapping(value = "updateUserName", method = RequestMethod.POST)
  public String updateyonghu(HttpServletRequest req,@Valid @ModelAttribute("UserB") UserB UserB, BindingResult results,Model model) throws SQLException {
  	  if(results.hasErrors()){
  		 
  		log.info("内容验证出错");
  		model.addAttribute("message", "密码或密码不能为空！");
  		UserB result=helloWorldService.selectnameid(UserB);
  	    model.addAttribute("UserB",result);  	
		return "update";
  	  }
  	
  	  String repassword = req.getParameter("repassword");
//  	  String password = req.getParameter("password");
//  	  String username=req.getParameter("username");
  
  	  
  	//用户名，密码不能为空时可以用这个
//    	if(username.isEmpty()&&password.isEmpty()){
//    		log.info("内容验证出错");
//    		UserB result=helloWorldService.selectnameid(UserB);
//    	    model.addAttribute("UserB",result);  	
//  		    return "update";
//    	  }
    	
//  	  if(username.isEmpty())
//  	  {
//		model.addAttribute("message", "用户名不能为空！");
//		UserB result=helloWorldService.selectnameid(UserB);
//     	model.addAttribute("UserB",result);  	
//		return "update";
//  	  }
//  	  if(password.isEmpty())
//  	  {
//		model.addAttribute("message", "密码不能为空");
//		UserB result=helloWorldService.selectnameid(UserB);
//  	    model.addAttribute("UserB",result);  	
//		return "update";
//  	  }
  	  if(!UserB.getPassword().equals(repassword))
  	  {
  		log.info("密码验证出错");
		model.addAttribute("message", "密码和密码确认必须一致！");
		UserB result=helloWorldService.selectnameid(UserB);
  	    model.addAttribute("UserB",result);  	
		return "update";
  	  }
  	  log.info("修改客户信息");
//      if(password.equals(repassword)){
      if(helloWorldService.updateYonghu(UserB)!=0)
      {
      	UserB result=helloWorldService.selectid(UserB);
      	model.addAttribute("UserB",result);  	
      	List<UserB> result1=helloWorldService.allyonghu(UserB);
  	    model.addAttribute("list",result1);
  		return "meeage";
  		}
  	else
  		return "update";
      }
//      else
//      {
////      	UserB result=helloWorldService.selectnameid(UserB);
////      	model.addAttribute("UserB",result);  	
////      	model.addAttribute("message", "密码与确认密码输入不正确");
//      	return "update";
//      }
  }

